package com.yyaccp.itrip.auth.service;

import com.yyaccp.itrip.beans.domain.ItripUser;
import com.yyaccp.itrip.dao.ItripUserMapper;
import com.yyaccp.itrip.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 天天向上 （john.yi@qq.com）
 * @date 2020/11/17.
 */
@Service
@Transactional
public class UserService {
    @Autowired
    private SMSService smsService;
    @Autowired
    private MailService mailService;
    @Autowired
    private ItripUserMapper itripUserMapper;

    public void regByPhone(ItripUser user) {
        // 1. 查询账号是否已经存在
        // boolean exists = itripUserMapper.exists(user.getUsercode());
        // 2. 保存用户到数据库
        user.setActivated(0);
//        itripUserMapper.insert(user);
        // 3. 发送短信
        smsService.send(user.getUserCode());
    }

    public void regByMail(ItripUser user) {
        // 1. 查询账号是否已经存在
        // boolean exists = itripUserMapper.exists(user.getUsercode());
        // 2. 保存用户到数据库
        user.setActivated(0);
//        itripUserMapper.insert(user);
        // 3. 发送邮件
        String code = RandomUtil.random(4);
        String content = "欢迎你注册爱旅行账号，你的激活码是" + code;
        String subject = "欢迎注册,请激活你的账号";
        mailService.send(code, user.getUserCode(), subject, content);
    }
}
